package com.longyiqun.cm.controller.scene;


import com.longyiqun.cm.PageUtil;
import com.longyiqun.cm.SSh_Connect;
import com.longyiqun.cm.TablePage;
import com.longyiqun.cm.domain.SceneEntity;
import com.longyiqun.cm.service.CasesService;
import com.longyiqun.cm.service.SceneService;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

import static com.longyiqun.cm.SSh_Connect.clean;

@RestController
@RequestMapping("/api/scene")
public class SceneRestControllerImpl {

    @Autowired
    PageUtil pageUtil;

    @Autowired
    SceneService sceneService;

    @RequestMapping("/SceneList")
    public String SceneList(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize, @RequestParam(required = false) String name,@RequestParam(required = false) String module,@RequestParam(required = false) String env){

        TablePage tablePage = pageUtil.getDataForPaging(sceneService.findAllScene(pageNumber,pageSize,name,module,env));
        return JSON.toJSONString(tablePage);
    }

    @RequestMapping("/APIList")
    public String APIList(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize,@RequestParam(required = false)String sceneID){

        TablePage tablePage = pageUtil.getDataForPaging(sceneService.findAPI(pageNumber,pageSize,sceneID));
        return JSON.toJSONString(tablePage);
    }

    @RequestMapping(value = "/addScene",method = RequestMethod.POST)
    public String addScene(@RequestBody SceneEntity sceneEntity){

        int result = sceneService.addScene(sceneEntity);
        if (result > 0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }

    }
    @RequestMapping(value = "/addAPI",method = RequestMethod.POST)
    public String addAPI(@RequestBody SceneEntity sceneEntity){

        String result = sceneService.addAPI(sceneEntity);
        return JSON.toJSONString(result);

    }

    @RequestMapping(value = "/updateScene",method = RequestMethod.POST)
    public String updateScene(@RequestBody SceneEntity sceneEntity){
        System.out.println(sceneEntity.getLeader()+"==========");
        int result = sceneService.updateScene(sceneEntity);
        if (result > 0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }
    @RequestMapping(value = "/updateAPI",method = RequestMethod.POST)
    public String updateAPI(@RequestBody SceneEntity sceneEntity){
        String result = sceneService.updateAPI(sceneEntity);
        return JSON.toJSONString(result);
    }
    @RequestMapping("/delApi")
    public String delApi(@RequestParam(required = false) String relationID){
        int result = sceneService.delAPI(relationID);
        System.out.println("++++++++++"+result);
        if (result > 0){
            return "true";
        }else {
            return "false";
        }
    }

    @RequestMapping("/apiSelect")
    public String apiSelect(){
        TablePage tablePage = pageUtil.getDataForPaging(sceneService.APISelect());
        return JSON.toJSONString(tablePage);
    }

    @RequestMapping("/sceneCopy")
    public String sceneCopy(@RequestParam(required = false) String sceneID){

        int result = sceneService.sceneCopy(sceneID);
        if (result > 0){
            return "true";
        }else {
            return "false";
        }
    }

    @Autowired
    CasesService casesService;

    @RequestMapping("/runScene")
    public String runScene(@RequestParam(required = false) String sceneIDs,@RequestParam(required = false) String env){
        System.out.println(env);
        env = "3";
        System.out.println(sceneIDs);
        if (sceneIDs.indexOf(",") != -1){
            String[] scene = sceneIDs.split(",");
            for (int i = 0;i<scene.length;i++){
                String id = sceneIDs.split(",")[i];
                casesService.runScene(id,env);
            }
        }else {
            casesService.runScene(sceneIDs,env);
        }

        return "true";
    }

    @Autowired
    SSh_Connect sSh_connect;


    @RequestMapping("/cleanMysql")
    public String cleanMysql(@RequestParam(required = false)String sceneIDs,@RequestParam(required = false)String env) throws SQLException,ClassNotFoundException{
       System.out.println(sceneIDs);
        if (sceneIDs.indexOf(",") != -1){
            String[] scene = sceneIDs.split(";");
            for (int i = 0;i<scene.length;i++){

                String id = sceneIDs.split(";")[i];
                System.out.println(id);
                String sql = sceneService.GetCleanSql(id,env);
                String result = sSh_connect.sitConnect(sql,env);
            }
        }else {
            String sql = sceneService.GetCleanSql(sceneIDs,env);
            String result = sSh_connect.sitConnect(sql,env);
        }

        return "true";
    }
}
